import request from '@/utils/axios'

// 用户和权限接口
export function login(data) {
  const post = {
    grant_type: 'password',
    client_id: 'platManagement',
    client_secret: 'user123',
    ...data,
  }
  return request({
    url: 'auth/oauth/token',
    method: 'post',
    data: post
  })
}

// export function getInfo(token) {
//   return request1({
//     url: '/user/info',
//     method: 'get',
//     params: { token }
//   })
// }

export function logout() {
  return request({
    url: '/user/logout',
    method: 'post'
  })
}

/**
 * 获取该用户所有的公司
 * @author LXBsmile
 * @date 2019-10-24
 * @returns {Array} 公司列表
 */
export function getComList() {
  return request({
    url: '/plat/work/drop/sys_client',
    method: 'get'
  }).then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}

/**
 * 公司下的所有站点
 * @author LXBsmile
 * @date 2019-10-24
 * @param {Number}} 公司id
 * @returns {Array} 站点列表
 */
export function getStations(id) {
  return request({
    url: 'plat/work/dropSubstation',
    method: 'get',
    params: { pkid: id }
  }).then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}

/**
 * 保存角色
 * @author LXBsmile
 * @date 2019-10-24
 * @param {Object} role 角色对象， 有roleid为编辑， 无为添加
 * @returns {Object} role对象
 */
export function editRoles(role) {
  return request.post('/plat/user/editRoles', role).then(res => {
    if (res.code === 1) {
      return res.data;
    } else {
      throw res.msg;
    }
  })
}

/**
 * 配置角色的权限
 * @author LXBsmile
 * @date 2019-10-24
 * @param {Number} roleid 角色id
 * @param {Array} rights 权限列表
 * @returns {Promise} then: 配置成功; catch: 配置失败
 */
export function giveRgight(roleid, rights) {
  const post = {
    roleid,
    rights: rights.join(',')
  }
  return request.post('/plat/user/giveRights', post).then(res => {
    if (res.code === 1) {
      return true;
    } else {
      throw res.msg;
    }
  })
}

/**
 * 获取角色的所有权限
 * @author LXBsmile
 * @date 2019-10-24
 * @param {Number} roleid
 * @returns {Array} 权限列表
 */
export function getRoleRights(roleid) {
  return request.get('/plat/user/queryRoleRight', { params: { roleid } }).then(res => {
    if (res.code === 1) {
      return res.data;
    } else {
      throw res.msg;
    }
  })
}

/**
 * 获取所有权限的资源列表
 * @author LXBsmile
 * @date 2019-10-24
 * @param {Number} rightId 权限id
 * @returns {Array} 资源列表
 */
export function queryRightRes(rightId) {
  return request.get('/plat/user/queryRightRes', { params: { rightId } }).then(res => {
    if (res.code === 1) {
      return res.data;
    } else {
      throw res.msg;
    }
  })
}

/**
 * 保存权限
 * @author LXBsmile
 * @date 2019-10-24
 * @param {Object} right 权限对象
 * @returns {Object} 保存后的对象 有rightid为编辑， 无为添加
 */
export function editRights(right) {
  return request.post('/plat/user/editRights', right).then(res => {
    if (res.code === 1) {
      return res.data;
    } else {
      throw res.msg;
    }
  })
}

/**
 * 配置权限的资源
 * @author LXBsmile
 * @date 2019-10-24
 * @param {Number} rightId 权限id
 * @param {Array} resIds 资源id
 * @returns {Promise} then: 成功，catch：失败
 */
export function giveRes(rightId, resIds) {
  const post = { rightId, resIds: resIds.join(',') }
  return request.post('/plat/user/giveRes', post).then(res => {
    if (res.code === 1) {
      return true;
    } else {
      throw res.msg;
    }
  })
}

/**
 * 保存或新增客户
 * @author LXBsmile
 * @date 2019-11-15
 * @param {Object} post 提交主体
 * @returns {Promise}
 */
export function editCom(post) {
  return request.post('/plat/sys/client/save', post).then(res => {
    if (res.code === 1) {
      return true;
    } else {
      throw res.msg;
    }
  })
}

/**
 * 删除公司
 * @author LXBsmile
 * @date 2019-11-15
 * @param {Number} pkid 站点id
 * @returns {Promise}
 */
export function deleteCom(pkid) {
  return request.get('/plat/sys/client/delete', { params: { pkid } }).then(res => {
    if (res.code === 1) {
      return true;
    } else {
      throw res.msg;
    }
  })
}

/**
 * 编辑或新增站点
 * @author LXBsmile
 * @date 2019-11-15
 * @param {Object} post 主体
 * @returns {Promise}
 */
export function editStation(post) {
  return request.post('/plat/sys/substation/save', post).then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}

/**
 * 删除站点
 * @author LXBsmile
 * @date 2019-11-15
 * @param {Number} pkid 站点id
 * @returns {Promise}
 */
export function deleteStation(substationId) {
  return request.get('/plat/sys/substation/delete', { params: { substationId } }).then(res => {
    if (res.code === 1) return true;
    else throw res.msg;
  })
}

/**
 * 获取站点位置列表
 * @author LXBsmile
 * @date 2019-11-25
 * @param {any} clientId 公司id
 * @returns {Array} 位置列表
 */
export function getStastionsMap(clientId) {
  return request.get('/plat/sys/substation/simpleList', { params: { clientId } }).then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}

export function upload(o) {
  const params = new FormData();
  params.append('file', o.file);
  return request({
    method: 'post',
    url: '/plat/user/upload',
    headers: { 'Content-Type': 'multipart/form-data' },
    data: params
  }).then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}

/**
 * 获取合同列表
 * @author LXBsmile
 * @date 2019-12-05
 * @param {Number} clientId 客户id
 * @returns {Array} 合同列表
 */
export function getContractList(clientId, conName) {
  const params = {
    clientId,
    conName
  }
  return request.get('/plat/isp/contract/list', { params }).then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}

/**
 * 保存或添加合同
 * @author LXBsmile
 * @date 2019-12-05
 * @param {Object} post 合同对象
 * @returns {Object} 保存和添加后的合同对象
 */
export function saveContract(post) {
  return request.post('plat/isp/contract/save', post).then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}

/**
 * 删除合同
 * @author LXBsmile
 * @date 2019-12-05
 * @param {Number} pkid 合同id
 * @returns {Boolean}
 */
export function deleteContract(pkid) {
  return request.get(`plat/isp/contract/delete?pkid=${pkid}`).then(res => {
    if (res.code === 1) return true;
    else throw res.msg;
  })
}

/**
 * 获取运维计划列表
 * @author LXBsmile
 * @date 2019-12-05
 * @param {Number} contractId 合同id
 * @returns {Array}
 */
export function getMaintainList(contractId) {
  return request.get(`plat/isp/maintenPlan/list?contractId=${contractId}`).then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}

/**
 * 保存或添加运维计划
 * @author LXBsmile
 * @date 2019-12-05
 * @param {Object} post 计划对象
 * @returns {Object} 保存后的对象
 */
export function saveMaintain(post) {
  return request.post('plat/isp/maintenPlan/save', post).then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}

/**
 * 删除运维计划
 * @author LXBsmile
 * @date 2019-12-05
 * @param {Number} pkid 计划id
 * @returns {Boolean}
 */
export function delMaintain(pkid) {
  return request.get(`plat/isp/maintenPlan/delete?pkid=${pkid}`).then(res => {
    if (res.code === 1) return true;
    else throw res.msg;
  })
}

/**
 * 获取运维计划类型列表
 * @author LXBsmile
 * @date 2019-12-05
 * @returns {Array}
 */
export function getTypes() {
  return request.get('plat/isp/maintenPlan/types').then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}

/**
 * 获取用户信息
 * @author LXBsmile
 * @date 2019-12-12
 * @returns {Object} 用户信息
 */
export function getUserInfo() {
  return request.get('plat/user/info').then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}

/**
 * 获取菜单
 * @author LXBsmile
 * @date 2019-12-12
 * @returns {Array}
 */
export function getMenus() {
  return request.get('/plat/sys/menus').then(res => {
    if (res.code === 1) return res.data;
    else throw res.msg;
  })
}
